package sk.klimo.prototyp.client;

import java.util.List;

import sk.klimo.prototyp.client.event.UpdateItemEvent;
import sk.klimo.prototyp.client.event.UpdateItemEventHandler;
import sk.klimo.prototyp.client.model.Invoice;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.TreeItem;

public class MainPanel extends Composite {

	private HeaderPane headerWidget;
	private FooterPane footerWidget;
	private LeftPane leftWidget;
	private final RightPane rightWidget;

	public MainPanel(final HeaderPane headerWidget,
			final FooterPane footerWidget, final LeftPane leftWidget,
			final RightPane rightWidget) {
		super();

		final InvoiceServiceAsync invoiceService = (InvoiceServiceAsync) GWT
				.create(InvoiceService.class);

		DockPanel dockPanel = new DockPanel();
		initWidget(dockPanel);

		this.headerWidget = headerWidget;
		this.footerWidget = footerWidget;
		this.leftWidget = leftWidget;
		this.rightWidget = rightWidget;

		dockPanel.setBorderWidth(0);
		dockPanel.setSize("100%", "100%");
		dockPanel.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
		dockPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		dockPanel.add(headerWidget, DockPanel.NORTH);
		dockPanel.setCellHeight(headerWidget, "30px");
		dockPanel.add(footerWidget, DockPanel.SOUTH);
		dockPanel.setCellHeight(footerWidget, "30px");
		HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
		splitPanel.setSplitPosition("150px");
		splitPanel.setLeftWidget(leftWidget);
		splitPanel.setRightWidget(rightWidget);
		dockPanel.add(splitPanel, DockPanel.CENTER);

		leftWidget.getTree().addSelectionHandler(
				new SelectionHandler<TreeItem>() {
					public void onSelection(SelectionEvent<TreeItem> event) {
						String id = event.getSelectedItem().getText();
						UpdateItemEvent e = new UpdateItemEvent(id);
						rightWidget.getForm().fireEvent(e);
						rightWidget.getItemsTable().fireEvent(e);
						rightWidget.getReportFrame().fireEvent(e);
					}
				});

		rightWidget.getForm().addUpdateItemHandler(
				new UpdateItemEventHandler() {
					public void onItemUpdated(final UpdateItemEvent event) {

						invoiceService.getInvoice(event.getId(),
								new AsyncCallback<Invoice>() {

									public void onFailure(Throwable t) {
										Window.alert(t.toString());
									}

									public void onSuccess(Invoice invoice) {
										rightWidget.getForm().getAdresa()
												.setText(invoice.getAdresa());
										rightWidget.getForm().getCislo()
												.setText(invoice.getCislo());
										// rightWidget
										// .getForm()
										// .getDatumSplatnosti()
										// .setText(
										// invoice
										// .getDatumSplatnosti());
										// rightWidget
										// .getForm()
										// .getDatumVystavenia()
										// .setText(
										// invoice
										// .getDatumVystavenia());
										rightWidget.getForm().getMeno()
												.setText(invoice.getMeno());
										rightWidget
												.getForm()
												.getPriezvisko()
												.setText(
														invoice.getPriezvisko());
									}

								});

					}
				});

		rightWidget.getItemsTable().addUpdateItemHandler(
				new UpdateItemEventHandler() {
					public void onItemUpdated(final UpdateItemEvent event) {

						invoiceService.getInvoice(event.getId(),
								new AsyncCallback<Invoice>() {

									public void onFailure(Throwable t) {
										Window.alert(t.toString());
									}

									public void onSuccess(Invoice invoice) {
										rightWidget.getItemsTable().setText(0,
												0, event.getId());
										rightWidget.getItemsTable().setText(0,
												1, event.getId());
										rightWidget.getItemsTable().setText(0,
												2, event.getId());
									}

								});

					}
				});

		rightWidget.getReportFrame().addUpdateItemHandler(
				new UpdateItemEventHandler() {
					public void onItemUpdated(final UpdateItemEvent event) {
						rightWidget.getReportFrame().setUrl(
								"/ratechprototype/report.spring?id="
										+ event.getId());

					}
				});

		invoiceService.getInvoices(new AsyncCallback<List<Invoice>>() {

			public void onFailure(Throwable t) {
				Window.alert(t.toString());
			}

			public void onSuccess(List<Invoice> listInvoices) {
				for (Invoice in : listInvoices) {
					leftWidget.addInvoice(in);
				}
			}
		});
	}

	public HeaderPane getHeaderWidget() {
		return headerWidget;
	}

	public FooterPane getFooterWidget() {
		return footerWidget;
	}

	public LeftPane getLeftWidget() {
		return leftWidget;
	}

	public RightPane getRightWidget() {
		return rightWidget;
	}
}
